package nio;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class HibernateServerHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("HibernateServerHandler-->channelRead");

		ByteBuf buf = (ByteBuf) msg;
		byte[] req = new byte[buf.readableBytes()];
		buf.readBytes(req);

		// int len = req.length;
		// for (int i = 0; i < len; i++) {
		// System.out.println("req-->" + i + "==" + req[i]);
		// }

		String body = new String(req, "UTF-8");
		System.out.println("server receive-->" + body);

		System.out.println("req[0]-->" + String.valueOf(req[0]));

		StringBuffer sb = new StringBuffer("");
		String message = "";
		byte[] result = null;
		ByteBuf resp = null;

		switch (req[0]) {
		case RequestCode.REQUEST_CODE_CONNECT:
			sb.append("The connection is successful");
			message = new String(sb);
			result = ByteUtil.assembly(message.getBytes(RequestCode.CHARSET_NAME), RequestCode.REQUEST_CODE_CONNECT);
			break;
		case RequestCode.REQUEST_CODE_REGISTER:
			sb.append("Registered successfully");
			message = new String(sb);
			result = ByteUtil.assembly(message.getBytes(RequestCode.CHARSET_NAME), RequestCode.REQUEST_CODE_REGISTER);
			break;
		case RequestCode.REQUEST_CODE_LOGIN:
			sb.append("Login successful");
			message = new String(sb);
			result = ByteUtil.assembly(message.getBytes(RequestCode.CHARSET_NAME), RequestCode.REQUEST_CODE_LOGIN);
			break;
		default:
			System.out.println("default-->");
			break;
		}

		if (result != null) {
			System.out.println("server send-->" + new String(result, "UTF-8"));
			resp = Unpooled.copiedBuffer(result);
		}

		if (resp != null) {
			ctx.writeAndFlush(resp);
		}

		// String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)
		// ? new java.util.Date(System.currentTimeMillis()).toString() : "BAD
		// ORDER";
		// ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
		// ctx.write(resp);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		System.out.println("HibernateServerHandler-->channelReadComplete");

		ctx.flush();
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		// TODO Auto-generated method stub
		super.userEventTriggered(ctx, evt);
		System.out.println("HibernateServerHandler-->userEventTriggered");

		if (evt instanceof IdleStateEvent) {
			IdleStateEvent e = (IdleStateEvent) evt;
			if (e.state() == IdleState.READER_IDLE) {
				System.out.println("userEventTriggered-->READER_IDLE");
			} else if (e.state() == IdleState.WRITER_IDLE) {
				System.out.println("userEventTriggered-->WRITER_IDLE");
			}
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		System.out.println("HibernateServerHandler exceptionCaught-->" + cause.getMessage());

		cause.printStackTrace();
		ctx.close();
	}

}
